package com.jule.miaozhuapc.webSocket.room;

import org.apache.log4j.Logger;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间列表webSocket
 */
public class WawaRoomUsedWebSocket  extends TextWebSocketHandler {

    private Logger logger = Logger.getLogger(WawaRoomUsedWebSocket.class);


    /**
     * 列表页用户
     */
    public static List<WebSocketSession> RoomDatas = new ArrayList<WebSocketSession>();

    /**
     * 连接成功时候，会触发页面上onopen方法
     */
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("列表页用户加 sessionId = " + session.getId());
        if(!RoomDatas.contains(session)){
            RoomDatas.add(session);
        }
    }

    /**
     * 关闭连接时触发
     */
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        logger.info("用户退出 sessionId = " + session.getId());
        removeUser(session);
    }

    /**
     * js调用websocket.send时候，会调用该方法
     */
    @SuppressWarnings({"unused", "unlikely-arg-type"})
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
        // 接收用户消息
        System.out.println("接收到消息:" + message);

    }

    /**
     * 异常
     */
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {

        logger.info("异常 sessionId = " + session.getId());
        removeUser(session);
    }

    public boolean supportsPartialMessages() {
        return false;
    }

    /**
     * 移除用户
     *
     * @param session
     * @throws Exception
     */
    @SuppressWarnings({"unused", "unlikely-arg-type"})
    private void removeUser(WebSocketSession session) throws Exception {
        // RoomDatas 移除用户
        if(RoomDatas.contains(session)){
            RoomDatas.remove(session);
        }

    }

//    //发送 房间使用情况 线程
//				new Thread(new Runnable() {
//        @Override
//        public void run() {
//            // 发送房间使用情况000
//            while(true) {
//                // 房间使用情况
//                String msg = new SocketCom().getRoomsGame();
//                List<Socket> roomsDa = new ArrayList<Socket>();
//                for (Socket sr : roomsData) {
//                    // 发送 通知，  如果返回false 异常
//                    if ( !new WawaSocketWrite().WriteOneSocket(sr, msg)) {
//                        roomsDa.add(sr);
//                    }
//                }
//                // 房间列表  socket 断开用户  del
//                for (Socket sd : roomsDa) {
//                    roomsData.remove(sd);
//                    // 关闭 socket
//                    new SocketCom(sd).closeSocket();
//                }
//                // 休息 10秒
//                try {
//                    Thread.sleep(3 * 1000);
//                } catch (InterruptedException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
//        }
//    }).start();
}
